# -*- coding: utf-8 -*-
"""
Created on Tue Jun  5 21:16:41 2018

@author: Administrator
"""
#%% being cautious
#!!!!!!!!!!!!!!!!!!!!!!
'''
if Fslvbm_1_bet   Fslvbm_2_template   Fslvbm_3_proc was failed or interrupted,
the code:'ln -sf /root/anaconda2/envs/py3/bin/python3.7  /root/anaconda2/envs/py3/bin/python'
must be run in the shell
'''
#!!!!!!!!!!!!!!!!!!!!!

#%%
import os
import glob
from ..origin import Shell_script


#%%

class Template_list(Shell_script):
    #作用原理：
    #参数说明：
    #参数说明：
    #返回值说明：
    #举例：
    #调用：
    #被调用：
    #bug：
    template_list_str = 'template_list'
    def __init__(self,input_list):
        print('input_list:',input_list)
        if len(input_list) != 2 :
            raise Exception("the number of elements of input_list is not equal to 3")
        super().__init__(input_list)
    def initialize_input_list(self,input_list):
        
        #重新整理传入参数
        self.input_dirpath = input_list[0]
        self.group_tag_list = input_list[1]
        
    def concat_inputs(self):
        all_groups_filenames_list = []
        #the code below is to get the filepaths of each group
        for tag in self.group_tag_list:
            
            blur_dirpath = self.input_dirpath+self.os_sep + tag + self.star + self.suffix_gz
            print('blur_dirpath:',blur_dirpath)
            input_filepaths = glob.glob(blur_dirpath)
            print('self.input_filepath:',input_filepaths)
            filenames_list = [os.path.split(i)[1]+self.space for i in input_filepaths]
            all_groups_filenames_list.append(filenames_list)
        length = [len(i) for i in all_groups_filenames_list]
        min_length = min(length)
        print('#################################################')
        print('min_length:',min_length)
        min_merge_filenames_list = [''.join(i[0:min_length]) for i in all_groups_filenames_list]
        self.filenames_str = ''.join(min_merge_filenames_list)
        print('self.filenames_str:',self.filenames_str)
    
    def concat_script(self):
        
        #cmd_1的内容：cd input_filepath
        self.cmd_1 =  r' cd ' + self.input_dirpath
        
        self.cmd_2 = r' echo ' + self.filenames_str + r' > ' + self.template_list_str
        
        self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2

#%%
        
class Fslvbm_1_bet(Shell_script):
    #作用原理：
    #参数说明：
    #参数说明：
    #返回值说明：
    #举例：
    #调用：
    #被调用：
    #bug：
    def __init__(self,input_list):
        print('input_list:',input_list)
        if len(input_list) != 2 :
            raise Exception("the number of elements of input_list is not equal to 2")
        super().__init__(input_list)
    def initialize_input_list(self,input_list):
        
        #重新整理传入参数
        self.input_dirpath = input_list[0]
        self.bet_para = input_list[1]
        
    def concat_script(self):
        
        #cmd_1的内容：cd input_filepath
        self.cmd_1 =  r' cd ' + self.input_dirpath

        #ln -sf /root/anaconda2/bin/python  /root/anaconda2/envs/py3/bin/python
        self.cmd_2 = ' ln -sf /root/anaconda2/bin/python  /root/anaconda2/envs/py3/bin/python '
        
        self.cmd_3 = r' fslvbm_1_bet ' + self.bet_para

        #ln -sf /root/anaconda2/envs/py3/bin/python3.7  /root/anaconda2/envs/py3/bin/python
        self.cmd_4 = r' ln -sf /root/anaconda2/envs/py3/bin/python3.7  /root/anaconda2/envs/py3/bin/python '
        
        self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2 + self.cmd_sep + self.cmd_3 \
                            + self.cmd_sep + self.cmd_4 

#%%

class Fslvbm_2_template(Shell_script):
    #作用原理：
    #参数说明：
    #参数说明：
    #返回值说明：
    #举例：
    #调用：
    #被调用：
    #bug：
    def __init__(self,input_list):
        print('input_list:',input_list)
        if len(input_list) != 2 :
            raise Exception("the number of elements of input_list is not equal to 2")
        super().__init__(input_list)
    def initialize_input_list(self,input_list):
        
        #重新整理传入参数
        self.input_dirpath = input_list[0]
        self.template_para = input_list[1]
        
    def concat_script(self):
        
        #cmd_1的内容：cd input_filepath
        self.cmd_1 =  r' cd ' + self.input_dirpath
        
        #ln -sf /root/anaconda2/bin/python  /root/anaconda2/envs/py3/bin/python
        self.cmd_2 = ' ln -sf /root/anaconda2/bin/python  /root/anaconda2/envs/py3/bin/python '
        
        self.cmd_3 = r' fslvbm_2_template ' + self.template_para
        
        #ln -sf /root/anaconda2/envs/py3/bin/python3.7  /root/anaconda2/envs/py3/bin/python
        self.cmd_4 = r' ln -sf /root/anaconda2/envs/py3/bin/python3.7  /root/anaconda2/envs/py3/bin/python '
        
        self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2 + self.cmd_sep + self.cmd_3 \
                            + self.cmd_sep + self.cmd_4 

#%%
        
class Fslvbm_3_proc(Shell_script):
    #作用原理：
    #参数说明：
    #参数说明：
    #返回值说明：
    #举例：
    #调用：
    #被调用：
    #bug：
    def __init__(self,input_list):
        print('input_list:',input_list)
        if len(input_list) != 1 :
            raise Exception("the number of elements of input_list is not equal to 1")
        super().__init__(input_list)
    def initialize_input_list(self,input_list):
        
        #重新整理传入参数
        self.input_dirpath = input_list[0]
        
    def concat_script(self):
        
        #cmd_1的内容：cd input_filepath
        self.cmd_1 =  r' cd ' + self.input_dirpath
        
        #ln -sf /root/anaconda2/bin/python  /root/anaconda2/envs/py3/bin/python
        self.cmd_2 = ' ln -sf /root/anaconda2/bin/python  /root/anaconda2/envs/py3/bin/python '
        
        self.cmd_3 = r' fslvbm_3_proc '
        #ln -sf /root/anaconda2/envs/py3/bin/python3.7  /root/anaconda2/envs/py3/bin/python
        
        self.cmd_4 = r' ln -sf /root/anaconda2/envs/py3/bin/python3.7  /root/anaconda2/envs/py3/bin/python '        
        
        self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2 + self.cmd_sep + self.cmd_3 \
                            + self.cmd_sep + self.cmd_4 

#%%

class Smoothing_sgima(Shell_script):
    #作用原理：
    #参数说明：
    #参数说明：
    #返回值说明：
    #举例：
    #调用：
    #被调用：
    #bug：
    file_str = 'GM_mod_merg_s'
    def __init__(self,input_list):
        print('input_list:',input_list)
        if len(input_list) != 2 :
            raise Exception("the number of elements of input_list is not equal to 3")
        super().__init__(input_list)
    def initialize_input_list(self,input_list):
        
        #重新整理传入参数
        self.input_dirpath = input_list[0]
        self.sigma = input_list[1]
        
    def concat_inputs(self):
        self.sigmafilename = self.file_str + self.sigma
    
    def concat_script(self):
        
        #cmd_1的内容：cd input_filepath
        self.cmd_1 =  r' cd ' + self.input_dirpath
        
        #fslmaths GM_mod_merg -s 3.5 GM_mod_merg_s3.5
        self.cmd_2 = r' fslmaths GM_mod_merg -s ' + self.sigma +  + self.sigmafilename
        
        self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2
    
#%%

class Randomise(Shell_script):
    #作用原理：
    #参数说明：
    #参数说明：
    #返回值说明：
    #举例：
    #调用：
    #被调用：
    #bug：
    fslvbm_str = 'fslvbm'
    gm_mask_filename = 'GM_mask'
    def __init__(self,input_list):
        print('input_list:',input_list)
        if len(input_list) != 3 :
            raise Exception("the number of elements of input_list is not equal to 3")
        super().__init__(input_list)
    def initialize_input_list(self,input_list):
        
        #重新整理传入参数
        self.input_dirpath = input_list[0]
        self.sigma_filename = input_list[1]
        self.permutation_num = input_list[2]
    
    def concat_script(self):
        
        #cmd_1的内容：cd input_filepath
        self.cmd_1 =  r' cd ' + self.input_dirpath
        
        #randomise -i ./stats/GM_mod_merg_s3 -o fslvbm -m ./stats/GM_mask  -d design.mat -t design.con -T -n 5000
        self.cmd_2 = r' randomise -i ./stats/' + self.sigma_filename + ' -o ./stats/' + self.fslvbm_str + \
                    ' -m ./stats/' + self.gm_mask_filename + ' -d ./design.mat -t ./design.con -T -n ' + self.permutation_num

        self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2


#%%